package cn.itsource.article.controller;


import cn.itsource.article.domain.ArticleType;
import cn.itsource.article.query.ArticleTypeQuery;
import cn.itsource.article.service.ArticleTypeService;
import cn.itsource.system.annotation.PermissionAnnotation;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articletype")
public class ArticleTypeController {

    @Autowired
    private ArticleTypeService articleTypeService;


    /**
     * 新增或修改
     * @param a
     * @return
     */
    @PostMapping
    @PermissionAnnotation(name="权限新增和修改",sn="permission:save")
    public AjaxResult save(@RequestBody ArticleType a){
        if (a.getId()==null){
            articleTypeService.insert(a);
            return AjaxResult.ok();
        }else {
            articleTypeService.updateByPrimaryKeySelective(a);
            return AjaxResult.ok();
        }
    }



    /**
     * 分页查询
     * @param articleTypeQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody ArticleTypeQuery articleTypeQuery){
        try {
            //System.out.println(articleTypeQuery);
            return AjaxResult.ok(articleTypeService.pageInfo(articleTypeQuery));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 批量删除
     * @param a
     * @return
     */
    @PatchMapping("/batchDeletion")
    public AjaxResult batchDeletion(@RequestBody List<ArticleType> a){

        try {
            articleTypeService.deleteAll(a);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            articleTypeService.delete(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 树形展示
     * @return
     */
   @PatchMapping("/gettree")
    public AjaxResult result(){
       try {
           return AjaxResult.ok(articleTypeService.gettree());
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
   }


    /**
     * 圆图
     * @param
     * @return
     */
    @GetMapping("/yuan")
    public AjaxResult drawPieChart() {
        List<ArticleType> namelist = articleTypeService.drawPieChart();
        return AjaxResult.ok(namelist);
    }
}
